package bkone;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class changebbookkss
 */
@WebServlet("/changebbookkss")
public class changebbookkss extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public changebbookkss() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: changebbookkss").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		String bktitlexg = "0";
		String bkcontentxg = "0";
		//String bktimesxg = "0";
		String bkidsxg = request.getParameter("bkidsxg");
		bktitlexg = request.getParameter("bktitlexg");
		bkcontentxg = request.getParameter("bkcontentxg");
		//bktimesxg = request.getParameter("bktimesxg");
		System.out.println("bkidsxg:" + bkidsxg);
		System.out.println("bktitlexg:" + bktitlexg);
		System.out.println("bkcontentxg:" + bkcontentxg);
		//System.out.println("bktimesxg:" + bktimesxg);
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection cont7;
		try {
			cont7 = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			PreparedStatement prestn7;
			PreparedStatement prestn8;
			//PreparedStatement prestn9;
			String updatetitle = "update blog set title = '" + bktitlexg + "' where id = ?";
			String updatecontent = "update blog set content = '" + bkcontentxg + "' where id = ?";
			//String updatetimes = "update blog set publish_time = '" + bktimesxg + "' where id = ?";
			prestn7 = cont7.prepareStatement(updatetitle);
			prestn8 = cont7.prepareStatement(updatecontent);
			//prestn9 = cont7.prepareStatement(updatetimes);
			prestn7.setString(1, bkidsxg);
			prestn8.setString(1, bkidsxg);
			//prestn9.setString(1, bkidsxg);
			if (!bkcontentxg.equals("")) {
				if (!bktitlexg.equals("")) {
					prestn7.executeUpdate();
					prestn8.executeUpdate();
				}
			}
			//if(!bktimesxg.equals("0")){
			//	prestn9.executeUpdate();
			//}
			System.out.println(bktitlexg.equals(""));
			System.out.println(bkcontentxg.equals(""));
			//System.out.println(bktimesxg.equals("0"));
			//prestn9.close();
			prestn8.close();
			prestn7.close();
			cont7.close();
			response.sendRedirect("/admin_bloggl.jsp");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
